# -*- coding:utf-8 -*-
import sys
import json
from decimal import Decimal
import MySQLdb
import datetime
def default(obj):
    if isinstance(obj, Decimal):
        return str(obj)
    raise TypeError
#连接数据库
dbstr=sys.argv[1]
city=sys.argv[2]
print dbstr
print city
d1 = datetime.datetime.now()
#conn=MySQLdb.connect(host='rds1tu038044m8zub4o7.mysql.rds.aliyuncs.com',user='zhuge',passwd='zhuge1116',db=dbstr,port=3306,charset="utf8")
conn_online=MySQLdb.connect(host='101.200.122.53',user='zhugef_online',passwd='emd3ZW56*$FuYmFanVuZmVuZw%db',db=dbstr,port=3307,charset="utf8")
#conn=MySQLdb.connect(host='182.92.96.120',user='zhugef_offline',passwd='2d573c729a&^%460adc4c5547ff7e7%8',db=dbstr,port=3306,charset="utf8")
#conn=MySQLdb.connect(host='101.200.122.53',user='zhugef_online',passwd='emd3ZW56*$FuYmFanVuZmVuZw%db',db=dbstr,port=3307,charset="utf8")
#cur = conn.cursor()
cur_online = conn_online.cursor()
#cur.execute('select count(*) from house_rent_gov')
cur_online.execute('select count(*) from house_rent_gov_new')
#获取数据库中记录总条数
count_gov_online=cur_online.fetchone()[0]
print "count_gov_online=",count_gov_online
#count_gov_offline=cur.fetchone()[0]
#print "count_gov_offline=",count_gov_offline
cur_online.execute('select count(*) from house_rent_info_new')
count_info_online=cur_online.fetchone()[0]
print "count_info_online=",count_info_online
cur_online.execute('select count(*) from house_rent_new')
count_rent_online=cur_online.fetchone()[0]
print "count_rent_online=",count_rent_online
cur_online.execute('select sum(cnt) from house_rent_new')
sum_cnt=cur_online.fetchone()[0]
print "sum_cnt=",sum_cnt

#if ((count_gov_online != count_gov_offline) or (count_gov_online != count_info_online)):
#    print "count not match"
    #sys.exit(1)
if (count_rent_online > count_info_online):
    print "rent bigger than info"
    sys.exit(1)
if (sum_cnt <= count_info_online):
    print "sum cnt less then info"
merge_rate=(count_gov_online-count_rent_online)*100/count_gov_online
print "merge_rate=", merge_rate
if ((merge_rate > 20) or (merge_rate < 2)):
    print "merge rate too low or too high"
    sys.exit(1)
cur_online = conn_online.cursor(cursorclass=MySQLdb.cursors.DictCursor)
sql="SELECT CONCAT(house_rent_new.id,','),CONCAT(house_rent_info_new.id,',') FROM house_rent_new LEFT JOIN  house_rent_info_new ON house_rent_new.id=house_rent_info_new.id WHERE house_rent_info_new.id is null"
cur_online.execute(sql)
results=cur_online.fetchall()
print results
if results != ():
    print "rent rent_info correlation not match"
    sys.exit(1)
